package com.sgcc.pda.bluetooth.utils;

import android.bluetooth.BluetoothDevice;
import android.text.TextUtils;

import com.clj.fastble.data.BleDevice;
import com.sgcc.pda.bluetooth.api.BlueDeviceEntity;

import static com.sgcc.pda.bluetooth.config.Constant.BLUE_BLE;
import static com.sgcc.pda.bluetooth.config.Constant.BLUE_CLS;

/**
 * 描 述：最近连接的设备信息处理工具类
 * 作 者：yangzhi
 * 时 间：2020/3/9 11:35
 * 版 本：1.0
 * 版 权： Copyright (c) remarkables-united.Co.Ltd. All rights reserved.
 */
public class BlueDeviceInfoUtil {

    //蓝牙类型
    private int type;
    //蓝牙信息
    private String deviceInfo;
    //蓝牙连接状态  1已连接, 2已断开
    private int connStatus;

    private static BlueDeviceInfoUtil mInstance = null;


    public static BlueDeviceInfoUtil getInstance() {
        synchronized (BlueDeviceInfoUtil.class) {
            if (null == mInstance) {
                mInstance = new BlueDeviceInfoUtil();
            }
        }
        return mInstance;
    }


    /**
     * 设置最近连接的设备
     *
     * @return
     */
    public void setDeviceInfo(int type, String device, int connStatus) {
        this.type = type;
        this.deviceInfo = device;
        this.connStatus = connStatus;
    }

    /**
     * 获取最近连接的ble蓝牙对象
     */
    public BleDevice getBleDevice() {
        if (!TextUtils.isEmpty(deviceInfo)) {
            try {
                BleDevice bleDevice = ToolGson.getBean(deviceInfo, BleDevice.class);
                if (null != bleDevice) {
                    return bleDevice;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }


    /**
     * 获取最近连接的经典蓝牙对象
     */
    public BluetoothDevice getClsDevice() {
        if (!TextUtils.isEmpty(deviceInfo)) {
            try {
                BluetoothDevice bluetoothDevice = ToolGson.getBean(deviceInfo, BluetoothDevice.class);
                if (null != bluetoothDevice) {
                    return bluetoothDevice;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }


    /**
     * 获取最近连接的设备
     */
    public BlueDeviceEntity getDeviceInfo() {
        if (!TextUtils.isEmpty(deviceInfo)) {
            BlueDeviceEntity blueDeviceEntity = new BlueDeviceEntity();
            if (BLUE_CLS == type) {
                //经典蓝牙
                BluetoothDevice bluetoothDevice = getClsDevice();
                if (null != bluetoothDevice) {
                    blueDeviceEntity.setName(bluetoothDevice.getName());
                    blueDeviceEntity.setMac(bluetoothDevice.getAddress());
                    blueDeviceEntity.setType(BLUE_CLS);
                    blueDeviceEntity.setConnStatus(connStatus);
                    return blueDeviceEntity;
                }

            } else if (BLUE_BLE == type) {
                //ble蓝牙
                BleDevice bleDevice = getBleDevice();
                if (null != bleDevice) {
                    blueDeviceEntity.setName(bleDevice.getName());
                    blueDeviceEntity.setMac(bleDevice.getMac());
                    blueDeviceEntity.setType(BLUE_BLE);
                    blueDeviceEntity.setConnStatus(connStatus);
                    return blueDeviceEntity;
                }
            }

        }
        return null;
    }


}
